<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="redis,redis集群," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.0" />






<meta name="description" content="之前搭建的哨兵模式，帮我们解决了服务的高可用性。但依然有如下几点不足：1、当我们的master挂机后，哨兵节点会给我们重新选举新的master，保证集群的可用。如果我们的sentinel哨兵节点宕机了呢，哨兵模式的解决方案是，我们会部署多个哨兵节点，来预防sentinel故障的情形。2、sentinel集群模式下，所有的写入压力都集中在master节点上，因此集群的性能瓶颈可能在master节点上">
<meta name="keywords" content="redis,redis集群">
<meta property="og:type" content="article">
<meta property="og:title" content="redis集群模式搭建">
<meta property="og:url" content="https://gitee.com/shuhao/2017/12/18/redis集群模式搭建/index.html">
<meta property="og:site_name" content="Niki">
<meta property="og:description" content="之前搭建的哨兵模式，帮我们解决了服务的高可用性。但依然有如下几点不足：1、当我们的master挂机后，哨兵节点会给我们重新选举新的master，保证集群的可用。如果我们的sentinel哨兵节点宕机了呢，哨兵模式的解决方案是，我们会部署多个哨兵节点，来预防sentinel故障的情形。2、sentinel集群模式下，所有的写入压力都集中在master节点上，因此集群的性能瓶颈可能在master节点上">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://gitee.com/blog/images/redis/cluster.jpg">
<meta property="og:updated_time" content="2017-12-23T01:57:43.547Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="redis集群模式搭建">
<meta name="twitter:description" content="之前搭建的哨兵模式，帮我们解决了服务的高可用性。但依然有如下几点不足：1、当我们的master挂机后，哨兵节点会给我们重新选举新的master，保证集群的可用。如果我们的sentinel哨兵节点宕机了呢，哨兵模式的解决方案是，我们会部署多个哨兵节点，来预防sentinel故障的情形。2、sentinel集群模式下，所有的写入压力都集中在master节点上，因此集群的性能瓶颈可能在master节点上">
<meta name="twitter:image" content="https://gitee.com/blog/images/redis/cluster.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/shuhao/2017/12/18/redis集群模式搭建/"/>





  <title> redis集群模式搭建 | Niki </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cb9f0585e406f878cb2f29c89ad3cdb7";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Niki</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">天道酬勤</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-categories"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/shuhao/blog/2017/12/18/redis集群模式搭建/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fang ShuHao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/head.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Niki">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                redis集群模式搭建
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-18T09:31:03+08:00">
                2017-12-18
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/redis/" itemprop="url" rel="index">
                    <span itemprop="name">redis</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="之前搭建的哨兵模式，帮我们解决了服务的高可用性。但依然有如下几点不足："><a href="#之前搭建的哨兵模式，帮我们解决了服务的高可用性。但依然有如下几点不足：" class="headerlink" title="之前搭建的哨兵模式，帮我们解决了服务的高可用性。但依然有如下几点不足："></a>之前搭建的哨兵模式，帮我们解决了服务的高可用性。但依然有如下几点不足：</h3><h2 id="1、当我们的master挂机后，哨兵节点会给我们重新选举新的master，保证集群的可用。如果我们的sentinel哨兵节点宕机了呢，哨兵模式的解决方案是，我们会部署多个哨兵节点，来预防sentinel故障的情形。"><a href="#1、当我们的master挂机后，哨兵节点会给我们重新选举新的master，保证集群的可用。如果我们的sentinel哨兵节点宕机了呢，哨兵模式的解决方案是，我们会部署多个哨兵节点，来预防sentinel故障的情形。" class="headerlink" title="1、当我们的master挂机后，哨兵节点会给我们重新选举新的master，保证集群的可用。如果我们的sentinel哨兵节点宕机了呢，哨兵模式的解决方案是，我们会部署多个哨兵节点，来预防sentinel故障的情形。"></a>1、当我们的master挂机后，哨兵节点会给我们重新选举新的master，保证集群的可用。如果我们的sentinel哨兵节点宕机了呢，哨兵模式的解决方案是，我们会部署多个哨兵节点，来预防sentinel故障的情形。</h2><h2 id="2、sentinel集群模式下，所有的写入压力都集中在master节点上，因此集群的性能瓶颈可能在master节点上。对此的解决方案有，我们可以引进写入性能较好的kv数据库如：ssd。另外一种解决思路是，我们是不是可以有多个master节点。"><a href="#2、sentinel集群模式下，所有的写入压力都集中在master节点上，因此集群的性能瓶颈可能在master节点上。对此的解决方案有，我们可以引进写入性能较好的kv数据库如：ssd。另外一种解决思路是，我们是不是可以有多个master节点。" class="headerlink" title="2、sentinel集群模式下，所有的写入压力都集中在master节点上，因此集群的性能瓶颈可能在master节点上。对此的解决方案有，我们可以引进写入性能较好的kv数据库如：ssd。另外一种解决思路是，我们是不是可以有多个master节点。"></a>2、sentinel集群模式下，所有的写入压力都集中在master节点上，因此集群的性能瓶颈可能在master节点上。对此的解决方案有，我们可以引进写入性能较好的kv数据库如：ssd。另外一种解决思路是，我们是不是可以有多个master节点。</h2><p>为了解决上面两个限制，redis退出了另一个集群模式cluster模式<br>1、cluster模式为P2P的去中心化模式，不存在类似sentinel哨兵节点宕机则无法管理集群的问题。（每个节点都有投票选举的权利）<br>2、cluster模式可以设置多个master节点，cluster模式会将所有的key映射为16383个slot，分配到master节点上。<br>3、cluster模式下的节点通过内部的二进制协议通信，以及交换数据。达到高效以及节省带宽的目的。<br>4、cluster集群模式下，判断一个节点宕机的条件是超过半数的节点ping访问超时限制。（超时限制的设置为cluster-node-timeout，默认为15000ms）</p>
<img src="/blog/images/redis/cluster.jpg">
<h2 id="搭建cluster模式"><a href="#搭建cluster模式" class="headerlink" title="搭建cluster模式"></a>搭建cluster模式</h2><h3 id="1、创建目录"><a href="#1、创建目录" class="headerlink" title="1、创建目录"></a>1、创建目录</h3><h6 id="创建redis-cluster目录"><a href="#创建redis-cluster目录" class="headerlink" title="创建redis-cluster目录"></a>创建redis-cluster目录</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">mkdir redis-cluster</span><br></pre></td></tr></table></figure>
<p>######创建6个节点目录（三主三从），以及数据目录和日志目录<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">mkdir -p redis-cluster/9001/data redis-cluster/9001/log redis-cluster/9002/data redis-cluster/9002/log redis-cluster/9003/data redis-cluster/9003/log redis-cluster/9004/data redis-cluster/9004/log redis-cluster/9005/data redis-cluster/9005/log redis-cluster/9006/data redis-cluster/9006/log</span><br></pre></td></tr></table></figure></p>
<p>######复制reids.conf到9001目录下<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">sudo cp /redis-4.0.2/redis.conf  /usr/local/redis/redis-cluster/9001</span><br></pre></td></tr></table></figure></p>
<h3 id="修改配置文件"><a href="#修改配置文件" class="headerlink" title="修改配置文件"></a>修改配置文件</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">port 9001</span><br><span class="line">dir /usr/local/redis/redis-cluster/9001/data</span><br><span class="line">logfile /usr/local/redis/redis-cluster/9001/log/9001.log</span><br><span class="line">pidfile /usr/local/redis/redis-cluster/9001/redis_9001.pid</span><br><span class="line">appendonly yes</span><br><span class="line">appendfilename appendonly_9001.aof</span><br><span class="line"></span><br><span class="line">cluster-enabled yes</span><br><span class="line">cluster-node-timeout 15000</span><br><span class="line">cluster-config-file nodes9001.conf</span><br></pre></td></tr></table></figure>
<p>主要是设置<em>cluster</em>选项的设置</p>
<h6 id="将9001目录中的配置文件拷贝到其他5个节点目录中"><a href="#将9001目录中的配置文件拷贝到其他5个节点目录中" class="headerlink" title="将9001目录中的配置文件拷贝到其他5个节点目录中"></a>将9001目录中的配置文件拷贝到其他5个节点目录中</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">sudo cp /usr/local/redis/redis-cluster/9001/redis.conf  /usr/local/redis/redis-cluster/9002</span><br><span class="line">sudo cp /usr/local/redis/redis-cluster/9001/redis.conf  /usr/local/redis/redis-cluster/9003</span><br><span class="line">sudo cp /usr/local/redis/redis-cluster/9001/redis.conf  /usr/local/redis/redis-cluster/9004</span><br><span class="line">sudo cp /usr/local/redis/redis-cluster/9001/redis.conf  /usr/local/redis/redis-cluster/9005</span><br><span class="line">sudo cp /usr/local/redis/redis-cluster/9001/redis.conf  /usr/local/redis/redis-cluster/9006</span><br></pre></td></tr></table></figure>
<h6 id="通过vim修改另外5个节点目录下的配置文件"><a href="#通过vim修改另外5个节点目录下的配置文件" class="headerlink" title="通过vim修改另外5个节点目录下的配置文件"></a>通过vim修改另外5个节点目录下的配置文件</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">vim /usr/local/redis/redis-cluster/9002/redis.conf</span><br><span class="line"></span><br><span class="line">#修改命令</span><br><span class="line">:0,$s/9001/9002/g</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<h3 id="构建cluster"><a href="#构建cluster" class="headerlink" title="构建cluster"></a>构建cluster</h3><h6 id="启动六个实例"><a href="#启动六个实例" class="headerlink" title="启动六个实例"></a>启动六个实例</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">sudo redis-server /usr/local/redis/redis-cluster/9001/redis.conf  </span><br><span class="line">sudo redis-server /usr/local/redis/redis-cluster/9001/redis.conf  </span><br><span class="line">sudo redis-server /usr/local/redis/redis-cluster/9001/redis.conf  </span><br><span class="line">sudo redis-server /usr/local/redis/redis-cluster/9001/redis.conf  </span><br><span class="line">sudo redis-server /usr/local/redis/redis-cluster/9001/redis.conf</span><br></pre></td></tr></table></figure>
<h6 id="安装集群所需的软件"><a href="#安装集群所需的软件" class="headerlink" title="安装集群所需的软件"></a>安装集群所需的软件</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">sudo apt-get install ruby</span><br><span class="line">sudo apt-get install rubygems</span><br><span class="line">gem install redis</span><br></pre></td></tr></table></figure>
<p>因为reids的cluster软件为ruby开发，因此需要安装ruby。</p>
<h6 id="配置cluster"><a href="#配置cluster" class="headerlink" title="配置cluster"></a>配置cluster</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">sudo /redis-4.0.2/src/redis-trib.rb  create --replicas 1 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003 127.0.0.1:9004 127.0.0.1:9005 127.0.0.1:9006</span><br><span class="line">&gt;&gt;&gt; Creating cluster</span><br><span class="line">&gt;&gt;&gt; Performing hash slots allocation on 6 nodes...</span><br><span class="line">Using 3 masters:</span><br><span class="line">127.0.0.1:9001</span><br><span class="line">127.0.0.1:9002</span><br><span class="line">127.0.0.1:9003</span><br><span class="line">Adding replica 127.0.0.1:9004 to 127.0.0.1:9001</span><br><span class="line">Adding replica 127.0.0.1:9005 to 127.0.0.1:9002</span><br><span class="line">Adding replica 127.0.0.1:9006 to 127.0.0.1:9003</span><br><span class="line">M: 1a316a85cd0330bd06cc68af55a9029c2826ea06 127.0.0.1:9001</span><br><span class="line">   slots:0-5460 (5461 slots) master</span><br><span class="line">M: 2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002</span><br><span class="line">   slots:5461-10922 (5462 slots) master</span><br><span class="line">M: a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003</span><br><span class="line">   slots:10923-16383 (5461 slots) master</span><br><span class="line">S: c3557afd64b0e913f9cac834c167565a3809b8fd 127.0.0.1:9004</span><br><span class="line">   replicates 1a316a85cd0330bd06cc68af55a9029c2826ea06</span><br><span class="line">S: 55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005</span><br><span class="line">   replicates 2a4c6b918e1ab5975541b0a1569c384833826ae7</span><br><span class="line">S: 923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006</span><br><span class="line">   replicates a0acca7be369c07be9a1cf7e20e73893ad07dae0</span><br><span class="line">Can I set the above configuration? (type &apos;yes&apos; to accept): yes</span><br><span class="line">&gt;&gt;&gt; Nodes configuration updated</span><br><span class="line">&gt;&gt;&gt; Assign a different config epoch to each node</span><br><span class="line">&gt;&gt;&gt; Sending CLUSTER MEET messages to join the cluster</span><br><span class="line">Waiting for the cluster to join....</span><br><span class="line">&gt;&gt;&gt; Performing Cluster Check (using node 127.0.0.1:9001)</span><br><span class="line">M: 1a316a85cd0330bd06cc68af55a9029c2826ea06 127.0.0.1:9001</span><br><span class="line">   slots:0-5460 (5461 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">M: a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003</span><br><span class="line">   slots:10923-16383 (5461 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">M: 2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002</span><br><span class="line">   slots:5461-10922 (5462 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: c3557afd64b0e913f9cac834c167565a3809b8fd 127.0.0.1:9004</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 1a316a85cd0330bd06cc68af55a9029c2826ea06</span><br><span class="line">S: 55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 2a4c6b918e1ab5975541b0a1569c384833826ae7</span><br><span class="line">S: 923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates a0acca7be369c07be9a1cf7e20e73893ad07dae0</span><br><span class="line">[OK] All nodes agree about slots configuration.</span><br><span class="line">&gt;&gt;&gt; Check for open slots...</span><br><span class="line">&gt;&gt;&gt; Check slots coverage...</span><br><span class="line">[OK] All 16384 slots covered.</span><br><span class="line">niki@niki-VirtualBox:/usr/local/redis/redis-cluster$</span><br></pre></td></tr></table></figure>
<p>如命令所示，在配置过程中 –replicas 1 表示主从节点比为一比一。配置cluster时，将9001,9002,9003设置为master节点，并打印出cluster其他信息，并询问你这样配置cluster是否可以。输入yes则创建cluster</p>
<h3 id="连接cluster节点，查看cluster信息"><a href="#连接cluster节点，查看cluster信息" class="headerlink" title="连接cluster节点，查看cluster信息"></a>连接cluster节点，查看cluster信息</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">127.0.0.1:9001&gt; redis-cli -c -p 9001</span><br><span class="line">127.0.0.1:9001&gt; cluster nodes</span><br><span class="line">1a316a85cd0330bd06cc68af55a9029c2826ea06 127.0.0.1:9001@19001 myself,master - 0 1513839989000 1 connected 0-5460</span><br><span class="line">a0acca7be369c07be9a1cf7e20e73893ad07dae0 127.0.0.1:9003@19003 master - 0 1513839990000 3 connected 10923-16383</span><br><span class="line">2a4c6b918e1ab5975541b0a1569c384833826ae7 127.0.0.1:9002@19002 master - 0 1513839988000 2 connected 5461-10922</span><br><span class="line">c3557afd64b0e913f9cac834c167565a3809b8fd 127.0.0.1:9004@19004 slave 1a316a85cd0330bd06cc68af55a9029c2826ea06 0 1513839990000 4 connected</span><br><span class="line">55951a248d8311bba0fa9975b7b1385fcd26b843 127.0.0.1:9005@19005 slave 2a4c6b918e1ab5975541b0a1569c384833826ae7 0 1513839991400 5 connected</span><br><span class="line">923603085f7bfe5d1fe539c286581e7321a69520 127.0.0.1:9006@19006 slave a0acca7be369c07be9a1cf7e20e73893ad07dae0 0 1513839990395 6 connected</span><br></pre></td></tr></table></figure>
<p>注意连接cluster中的节点时需要加上参数 <em>-c</em></p>
<p>从上面的信息可以看到：<br>redis-cluster将0-5460的slot分配到9001， 5461-10922分配到9002， 10923-16383分配到9003。差不多是平均分配。<br>9004,9005,9006为slave节点。</p>
<p><em>好了，我们的cluster模式搭建好了</em><br>剩下的需要让cluster发挥效果还需要处理，动态扩容，增加节点，重新分配slot大小等</p>
<p>注：<br>redis cluster命令<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">## 集群（cluster）</span><br><span class="line">cluster info</span><br><span class="line">cluster nodes</span><br><span class="line"></span><br><span class="line">##节点（node）</span><br><span class="line">cluster meet ip port  #将制定的节点添加到集群中，成为集群的一份子</span><br><span class="line">cluster forget node_id 从集群中移除node_id指定的节点</span><br><span class="line">cluster replicate node_id 将当前节点设置为node_id指定节点的从节点</span><br><span class="line">cluster saveconfig 将当前节点的配置文件保存到硬盘</span><br><span class="line"></span><br><span class="line">##槽（slot）</span><br><span class="line">cluster addslots &lt;slot&gt; [slot ...] 将一个或多个槽slot指派给当前节点</span><br><span class="line">cluster delslots &lt;slot&gt; [slot ...] 移除一个或多个槽对当前节点的指派</span><br><span class="line">cluster flushslots 移除指派给当前节点的所有槽，让当前节点变成一个没有指派任何槽的节点</span><br><span class="line">cluster setslot &lt;slot&gt; NODE &lt;node_id&gt; 将槽slot指派给node_id指定的节点，如果槽已经指派给另一个节点，那么先让另一个节点删除该槽，再进行指派</span><br><span class="line">cluster setslot &lt;slot&gt; migrating &lt;node_id&gt; 将本届点的槽slot迁移到node_id指定的节点中</span><br><span class="line">cluster setslot &lt;slot&gt; importing &lt;node_id&gt; 从node_id指定的节点中导入槽slot到本节点</span><br><span class="line">cluster setslot &lt;slot&gt; stable 取消对slot的导入（import）或者迁移（migrate）</span><br><span class="line"></span><br><span class="line">##键（key）</span><br><span class="line">cluster keyslot &lt;key&gt; 计算键key应该被放置在那个槽上</span><br><span class="line">cluster countkeysinslot &lt;slot&gt; 返回槽slot目前包含的键值对数量</span><br><span class="line">cluster getkeysinslot &lt;slot&gt; &lt;count&gt; 返回count个slot槽中的键</span><br></pre></td></tr></table></figure></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/redis/" rel="tag"># redis</a>
          
            <a href="/blog/tags/redis集群/" rel="tag"># redis集群</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2017/12/18/redis哨兵模式搭建/" rel="next" title="redis哨兵模式搭建">
                <i class="fa fa-chevron-left"></i> redis哨兵模式搭建
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2017/12/19/redis主从模式搭建/" rel="prev" title="redis主从模式搭建">
                redis主从模式搭建 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNjY5NS8xMzIzMA=="></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/head.jpg"
               alt="Fang ShuHao" />
          <p class="site-author-name" itemprop="name">Fang ShuHao</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives">
                <span class="site-state-item-count">135</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories">
                <span class="site-state-item-count">35</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/blog/tags">
                <span class="site-state-item-count">93</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#之前搭建的哨兵模式，帮我们解决了服务的高可用性。但依然有如下几点不足："><span class="nav-number">1.</span> <span class="nav-text">之前搭建的哨兵模式，帮我们解决了服务的高可用性。但依然有如下几点不足：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1、当我们的master挂机后，哨兵节点会给我们重新选举新的master，保证集群的可用。如果我们的sentinel哨兵节点宕机了呢，哨兵模式的解决方案是，我们会部署多个哨兵节点，来预防sentinel故障的情形。"><span class="nav-number"></span> <span class="nav-text">1、当我们的master挂机后，哨兵节点会给我们重新选举新的master，保证集群的可用。如果我们的sentinel哨兵节点宕机了呢，哨兵模式的解决方案是，我们会部署多个哨兵节点，来预防sentinel故障的情形。</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2、sentinel集群模式下，所有的写入压力都集中在master节点上，因此集群的性能瓶颈可能在master节点上。对此的解决方案有，我们可以引进写入性能较好的kv数据库如：ssd。另外一种解决思路是，我们是不是可以有多个master节点。"><span class="nav-number"></span> <span class="nav-text">2、sentinel集群模式下，所有的写入压力都集中在master节点上，因此集群的性能瓶颈可能在master节点上。对此的解决方案有，我们可以引进写入性能较好的kv数据库如：ssd。另外一种解决思路是，我们是不是可以有多个master节点。</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#搭建cluster模式"><span class="nav-number"></span> <span class="nav-text">搭建cluster模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、创建目录"><span class="nav-number">1.</span> <span class="nav-text">1、创建目录</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#创建redis-cluster目录"><span class="nav-number">1.0.0.1.</span> <span class="nav-text">创建redis-cluster目录</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#修改配置文件"><span class="nav-number">2.</span> <span class="nav-text">修改配置文件</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#将9001目录中的配置文件拷贝到其他5个节点目录中"><span class="nav-number">2.0.0.1.</span> <span class="nav-text">将9001目录中的配置文件拷贝到其他5个节点目录中</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#通过vim修改另外5个节点目录下的配置文件"><span class="nav-number">2.0.0.2.</span> <span class="nav-text">通过vim修改另外5个节点目录下的配置文件</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#构建cluster"><span class="nav-number">3.</span> <span class="nav-text">构建cluster</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#启动六个实例"><span class="nav-number">3.0.0.1.</span> <span class="nav-text">启动六个实例</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#安装集群所需的软件"><span class="nav-number">3.0.0.2.</span> <span class="nav-text">安装集群所需的软件</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#配置cluster"><span class="nav-number">3.0.0.3.</span> <span class="nav-text">配置cluster</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#连接cluster节点，查看cluster信息"><span class="nav-number">4.</span> <span class="nav-text">连接cluster节点，查看cluster信息</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Fang ShuHao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  



  
  

  

  

  

  


  

</body>
</html>
